package com.base.context.event;

import com.google.common.eventbus.EventBus;

/**
 * eventBus
 * @author Jason
 *
 */
public class EventUtils {

    private static final EventBus eventBus = new EventBus();

    public static void register(Object listener) {
        eventBus.register(listener);
    }

    /**
     * 默认异步
     * @param event
     */
    public static void post(Object event) {
        post(event, true);
    }

    /**
     * 
     * @param event
     * @param isAsyn false:同步 true:异步
     */
    public static void post(Object event, Boolean isAsyn) {
        if (true == isAsyn) {
            Thread thread = new Thread(new AsynEventPost(event));
            thread.setDaemon(true);//设置成true守护进程，false用户进程
            thread.start();
        } else {
            eventBus.post(event);
        }
    }

    public static EventBus getEventBus() {
        return eventBus;
    }

}

/**
 * 
 * @author tianchang
 *	异步提交event post
 */
class AsynEventPost implements Runnable {

    Object event = null;

    public AsynEventPost(Object event) {
        this.event = event;
    }

    public void run() {
        EventUtils.getEventBus().post(event);
    }

}
